<?php
/**
 * Created by JieHuiLong.
 * E-mail: wzm
 * Date: 2016/8/16
 * Time: 16:00
 * 微信模型
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\AdminUser as User;

class WechatGroup extends Model
{
	protected $table = 'wechat_groups';
	public static function getWechatGroupCids($uid,$field='*'){
		$userids[]['id']=$uid;
		$pids[]=$uid;
		do{
			$pids = User::select('id')->whereIn('pid',$pids)->get()->toArray();
			if(count($pids)>0)$userids = array_merge($userids,$pids);
		}while(count($pids)>0);
		$wuserids=WechatGroupUser::select('gid')->where('uid',$uid)->get()->toArray();
		//查询所有属于我及下级的分组
		return WechatGroup::select($field)
					->where(function($query) use($userids){
						$query->whereIn('wechat_groups.uid',$userids);//属于我及下级的id
					})
					->orWhere(function($query) use ($wuserids){ //授权给我的id
						$query->whereIn('wechat_groups.id',$wuserids);
					})
					->get()->toArray();
	}

}
